home *** CD-ROM | disk | FTP | other *** search
- /*---------------------------------------------------------------------*
- FILENAME: SERIAL.H
-
- Some definitions used by SERIAL.C
-
- *--------------------------------------------------------------------*/
-
- #define COM1 1
- #define COM2 2
- #define COM1BASE 0x3F8 /* Base port address for COM1 */
- #define COM2BASE 0x2F8 /* Base port address for COM2 */
-
- /*
- The 8250 UART has 10 registers accessible through 7 port addresses.
- Here are their addresses relative to COM1BASE and COM2BASE. Note
- that the baud rate registers, (DLL) and (DLH) are active only when
- the Divisor-Latch ccess-Bit (DLAB) is on. The (DLAB) is bit 7 of
- the (LCR).
-
- o TXR Output data to the serial port.
- o RXR Input data from the serial port.
- o LCR Initialize the serial port.
- o IER Controls interrupt generation.
- o IIR Identifies interrupts.
- o MCR Send contorl signals to the modem.
- o LSR Monitor the status of the serial port.
- o MSR Receive status of the modem.
- o DLL Low byte of baud rate divisor.
- o DHH Hefine ASCII 0x007F // Mask ASCII characters
- #define SBUFSIZ 0x4000 // Serial buffer size
-
- int SError = NOERROR;
- int portbase = 0;
-
- // A user defined type for use with getvect() & setvect()
- typedef void interrupt far (*intvect)(...);
-
- intvect oldvects[2];
-
- static char ccbuf[SBUFSIZ];
- unsigned int startbuf = 0;
- unsigned int endbuf = 0;
-
- /*-------------------------------------------------------------------
- com_int - Handle communications interrupts and put them in ccbuf
- */
-
- void interrupt com_int(void) {
- disable();
- if ((inportb(portbase + IIR) & RX_MASK) == RX_ID) {
- if (((endbuf + 1) & SBUFSIZ - 1) == startbuf)
- SError = BUFOVFL;
-
- ccbuf[endbuf++] = inportb(portbase + RXR);
- endbuf &= SBUFSIZ - 1;
- }
-
- // Signal end of hardware interrupt
- outportb(ICR, EOI);
- enable();
- } // end of com_int()
-
- /*-------------------------------------------------------------------
- SerialOut - Output a character to the serial port
- */
-
- int SerialOut(char x) {
- long int timeout = 0x0000FFFFL;
-
- outportb(portbase + MCR, MC_INT | DTR | RTS);
-
- // Wait for Clear To Send from modem
- while ((inportb(portbase + MSR) & CTS) == 0)
- if (!(--timeout))
- return (-1);
-
- timeout = 0x0000FFFFL;
-
- // Wait for transmitter to clear
- while ((inportb(portbase + LSR) & XMTRDY) == 0)
- if (!(--timeout))
- return (-1);
-
- disable();
- outportb(portbase + TXR, x);
- enable();
-
- return (0);
- } // end of serialOut()
-
- /*-------------------------------------------------------------------
- SerialString - Output a string to the serial port
- */
-
- void SerialString(char *string) {
- while (*string)
- SerialOut(*string++);
- } // end of SerailString()
-
- /*-------------------------------------------------------------------
- getccb - This routine returns the current value in the buffer
- */
-
- int getccb(void) {
- int res;
-
- if (endbuf == startbuf)
- return (-1);
-
- res = (int) ccbuf[startbuf++];
- startbuf %= SBUFSIZ;
- return (res);
- } // end of getccb()
-
- /*-------------------------------------------------------------------
- setvects - Install our functions to handle communications
- */
-
- void setvects(void) {
- oldvects[0] = (intvect) getvect(0x0B);
- oldvects[1] = (intvect) getvect(0x0C);
- setvect(0x0B, (intvect)com_int);
- setvect(0x0C, (intvect)com_int);
- } // end of setvects()
-
- /*-------------------------------------------------------------------
- resvects - Uninstall our vectors before exiting the program
- */
-
- void resvects(void) {
- setvect(0x0B, (intvect)oldvects[0]);
- setvect(0x0C, (intvect)oldvects[1]);
- } // end of resvects()
-
- /*-------------------------------------------------------------------
- i_enable - Turn on communications interrupts
- */
-
- void i_enable(int pnum) {
- int c;
-
- disable();
- c = inportb(portbase + MCR) | MC_INT;
- outportb(portbase + MCR, c);
- outportb(portbase + IER, RX_INT);
- c = inportb(IMR) & (pnum == COM1 ? IRQ4 : IRQ3);
- outportb(IMR, c);
- enable();
- } // end of i_enable()
-
- /*-------------------------------------------------------------------
- i_disable - Turn off communications interrupts
- */
-
- void i_disable(void) {
- int c;
-
- disable();
- c = inportb(IMR) | ~IRQ3 | ~IRQ4;
- outportb(IMR, c);
- outportb(portbase + IER, 0);
- c = inportb(portbase + MCR) & ~MC_INT;
- outportb(portbase + MCR, c);
- enable();
- } // end of i_disable()
-
- /*-------------------------------------------------------------------
- comm_on - Tell modem that we're ready to go
- */
-
- void comm_on(void) {
- int c, pnum;
-
- pnum = (portbase == COM1BASE ? COM1 : COM2);
- i_enable(pnum);
- c = inportb(portbase + MCR) | DTR | RTS;
- outportb(portbase + MCR, c);
- } // end of comm_on()
-
- /*-------------------------------------------------------------------
- comm_off - Go off-line
- */
-
- void comm_off(void) {
- i_disable();
- outportb(portbase + MCR, 0);
- } // end of comm_off()
-
- /*-------------------------------------------------------------------
- initserial -
- */
-
- void initserial(void) {
- endbuf = startbuf = 0;
- setvects();
- comm_on();
- } // end of initserial()
-
- /*-------------------------------------------------------------------
- closeserial -
- */
-
- void closeserial(void) {
- comm_off();
- resvects();
- } // end of closerial()
-
- /*-------------------------------------------------------------------
- SetPort - Set the port number to use
- */
-
- int SetPort(int Port) {
- int Offset, far *RS232_Addr;
-
- switch (Port) { // Sort out the base address
- caserupt only if it
- is not masked (FALSE).
- */
- #define IRQ3 0xF7 /* COM2 */
- #define IRQ4 0xEF /* COM1 */
- pt */
-
-
- /*
- The (IMR) tells the (PIC) to service an interrupt only if it
- is not masked (FALSE).
- */
- #define IRQ3 0xF7 /* COM2 */
- #define IRQ4 0xEF /* COM1 */